package weaver.interfaces.xfjt.action;


import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-10-10
 * @Description: 6.流程归档后，需要计算出每一行物品领用的总价格写入【办公用品申领台账】----计算价格需要依据：先入先出的规则计算价格
 * 7.流程归档后，扣除的【办公用品入库登记】里面那个记录，将信息写入明细表，便于后面反查。
 * @ModifyLog:
 **/
public class WeaBgypSllcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        writeLog(">>>>>WeaBgypSllcAction>>>>>requestid="+requestid);
        try {
            rs.executeQuery("select id,ffsj,slr,slrq from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            //发放时间
            String ffsj = Util.null2String(rs.getString("ffsj"));
            //申领人
            String slr = Util.null2String(rs.getString("slr"));
            //申领日期
            String slrq = Util.null2String(rs.getString("slrq"));

            rs.executeQuery("select mcll,mc,lb,slsl from "+tablename+"_dt1 where mainid=?",mainid);
            while (rs.next()) {
                //名称
                String mcll = Util.null2String(rs.getString("mcll"));
                String mc = Util.null2String(rs.getString("mc"));
                //申领数量
                int slsl = Util.getIntValue(rs.getString("slsl"));
                //申领数量
                String lb = Util.null2String(rs.getString("lb"));
                int count;
                int xslsl = slsl;
                rs1.executeQuery("select id,sl,ylysl,dj,rksj,(sl-isnull(ylysl,0)-isnull(ybfsl,0)) sysl " +
                        "from uf_bgyprkmx where wpmc=? and wplb=? and (sfsc is null or sfsc=1) and (sl-isnull(ylysl,0)-isnull(ybfsl,0))>0 " +
                        " order by rksj", mcll,lb);
                while (rs1.next()) {
                    int id = Util.getIntValue(rs1.getString("id"));
                    int sysl = Util.getIntValue(rs1.getString("sysl"),0);
                    rs1.writeLog(">>>>id="+id+",sysl="+sysl+",slsl="+slsl);
                    count = xslsl - sysl;
                    if (count > 0) {
                        xslsl = xslsl - sysl;
                        rs1.executeUpdate("insert into uf_bgyprkmx_dt1(mainid,ffsj,slr,slrq,sllc,slsl) " +
                                "values (?,?,?,?,?,?)",id,ffsj,slr,slrq, requestid,sysl);
                        rs1.executeUpdate("update uf_bgyprkmx set ylysl=isnull(ylysl,0)+"+sysl+" where id=?",id);
                    } else {
                        rs1.executeUpdate("insert into uf_bgyprkmx_dt1(mainid,ffsj,slr,slrq,sllc,slsl) " +
                                "values (?,?,?,?,?,?)",id,ffsj,slr,slrq, requestid,xslsl);
                        rs1.executeUpdate("update uf_bgyprkmx set ylysl=isnull(ylysl,0)+"+xslsl+" where id=?",id);
                        break;
                    }

                }
                rs.executeUpdate("update uf_bgypkc set kc=kc-"+slsl+" where id=?",mc);
            }
        return Action.SUCCESS;
    } catch(
    Exception e)

    {
        e.printStackTrace();
        requestInfo.getRequestManager().setMessageid("111111");
        requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
        return Action.FAILURE_AND_CONTINUE;
    }
}

}
